package com.ruoyi.web.controller.modules;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.stock.Retrieval;
import com.ruoyi.web.pojo.dto.stock.RetrievalExcel;
import com.ruoyi.web.pojo.dto.stock.RetrievalRep;
import com.ruoyi.web.pojo.request.stock.RetrievalAddReq;
import com.ruoyi.web.service.stock.IRetrievalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 出库明细Controller
 * 
 * @author zq
 * @date 2024-01-08
 */
@Api(value = "出库明细", tags = "出库明细接口")
@RestController
@RequestMapping("/retrieval")
public class RetrievalController extends BaseController
{

    private final IRetrievalService retrievalService;

    public RetrievalController(IRetrievalService retrievalService) {
        this.retrievalService = retrievalService;
    }

    /**
     * 查询出库明细列表
     */
    @ApiOperation("查询出库明细列表")
    @GetMapping("/list")
    public TableDataInfo<RetrievalRep> list(Retrieval retrieval)
    {
        startPage();
        List<RetrievalRep> list = retrievalService.selectRetrievalList(retrieval);
        if (CollectionUtils.isEmpty(list)){
            return  getDataTable(new ArrayList<>());
        }
       // list.forEach(s->s.setUserId(SecurityUtils.getLoginUser().getUser().getPhonenumber()));
        return getDataTable(list);
    }

    /**
     * 导出出库明细列表
     */
    @ApiOperation("导出出库明细列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody Retrieval retrieval) throws Exception {

        List<RetrievalRep> list = retrievalService.selectRetrievalList(retrieval);
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        List<RetrievalExcel> collect = list.stream().map(RetrievalExcel::new).collect(Collectors.toList());
        ExcelUtil<RetrievalExcel> util = new ExcelUtil<>(RetrievalExcel.class);
//        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("出库明细数据"+ DateUtils.datePath() +".xlsx", StandardCharsets.UTF_8.name()));
        util.exportExcel(response, collect, "出库明细数据");
    }

    /**
     * 获取出库明细详细信息
     */
    @ApiOperation("获取出库明细详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Retrieval retrieval = retrievalService.selectRetrievalById(id);
        if (Objects.isNull(retrieval)){
            return success();
        }
        return success(new RetrievalRep(retrieval));
    }

    /**
     * 新增出库
     */
    @ApiOperation("新增出库")
    @RateLimiter(time = 1,count = 1)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody RetrievalAddReq req)
    {
        return toAjax(retrievalService.insertRetrieval(Retrieval.toRetrieval(req)));
    }

}
